from flask import Blueprint, request, jsonify, g
from dao.user_dao import user_login, user_register, user_name_check, update_msg_type, query_msg_type, set_phone, \
    set_email, query_phone, query_email, query_user_info, update_user_info, query_baby_choose, query_baby_choose_name, \
    update_baby_choose
from util.jwt_util import generate_jwt_token
from util.resp import APIResponse
from vo.user_login_vo import UserLoginVO

user_blueprint = Blueprint('user', __name__)


# 用户登录
@user_blueprint.route('/login', methods=['POST'])
def login():
    # 打印日志
    print('login')
    data = request.get_json()
    username = data.get('username')
    password = data.get('password')
    user = user_login(username)
    if user:
        if user['password'] == password:

            # 返回用户登录信息：用户名+令牌
            vo = UserLoginVO(user['user_name'], generate_jwt_token(
                {'id': user['user_id'], 'msg_type': user['msg_type']}
            )
                             ).to_dict()
            return APIResponse.ok(data=vo)
        else:
            return APIResponse.error(message='密码错误', code=10001)
    else:
        return APIResponse.error(message='用户不存在', code=10002)


# 用户注册
@user_blueprint.route('/register', methods=['POST'])
def register():
    print('register')
    data = request.get_json()
    username = data.get('username')
    password = data.get('password')
    password2 = data.get('confirmPassword')

    # 判断两次密码是否一致
    if password != password2:
        return APIResponse.error(message='两次密码不一致', code=10004)

    # 判断用户名是否已存在
    check = user_name_check(username)
    if check:
        return APIResponse.error(message='用户名已存在', code=10005)

    user = user_register(username, password)
    if user:
        return APIResponse.ok(data=user)
    else:
        return APIResponse.error(message='注册失败', code=10003)


# 获取用户信息
@user_blueprint.route('/get-user-info', methods=['GET'])
def get_user_info():
    user_id = g.user['id']
    user = query_user_info()
    if user:
        return APIResponse.ok(data={'username': user['user_name'], 'password': user['password']})
    return APIResponse.error(code=10009, message='获取用户信息失败')


# 修改用户信息
@user_blueprint.route('/update-user-info', methods=['POST'])
def set_user_info():
    data = request.get_json()
    username = data.get('username')
    password = data.get('password')
    user = update_user_info(username, password)
    return APIResponse.ok(data=user)


# 获取选中检测的宝宝
@user_blueprint.route('/get-baby-choose', methods=['GET'])
def get_baby_choose():
    baby = query_baby_choose()
    if baby:
        return APIResponse.ok(data=baby)
    return APIResponse.error(code=10010, message='获取选中检测的宝宝失败')


# 获取选中宝宝的名称
@user_blueprint.route('/get-baby-choose-name', methods=['GET'])
def get_baby_choose_name():
    baby = query_baby_choose_name()
    if baby:
        return APIResponse.ok(data=baby)
    return APIResponse.error(code=10010, message='获取选中检测的宝宝失败')


# 设置选中检测的宝宝
@user_blueprint.route('/set-baby-choose', methods=['POST'])
def set_baby_choose():
    data = request.get_json()
    baby_id = data.get('id')
    baby = update_baby_choose(baby_id)
    return APIResponse.ok(data=baby)


# 获取默认提醒方式
@user_blueprint.route('/msg-type', methods=['GET'])
def get_msg_type():
    msg_type = query_msg_type()
    if msg_type:
        return APIResponse.ok(data=msg_type)
    return APIResponse.error(code=10006, message='获取提醒方式失败')


# 设置提醒方式
@user_blueprint.route('/setRemind', methods=['POST'])
def set_remind():
    data = request.get_json()
    print(data)
    msg_type = data.get('msg_type')

    remind = update_msg_type(msg_type)
    if remind:
        return APIResponse.ok(data=remind)
    return APIResponse.error(code=10006, message='设置提醒方式失败')


# 警报提醒
@user_blueprint.route('/remind', methods=['POST'])
def remind():
    user_id = g.user['id']
    msg_type = g.user['msg_type']
    rest = do_remind(msg_type)
    if rest:
        return APIResponse.ok(data=str(rest))

    return APIResponse.ok()


# 根据提醒方式进行提醒
def do_remind(msg_type):
    if msg_type == '1':
        # 发送语音
        print('发送语音')
        return 1
    if msg_type == '2':
        # 发送短信
        print('发送短信')
        return 2
    elif msg_type == '3':
        # 发送邮件
        print('发送邮件')
        return 3


# 查询绑定手机
@user_blueprint.route('/get-bind-phone', methods=['GET'])
def get_phone():
    phone = query_phone()
    if phone:
        return APIResponse.ok(data=phone)
    return APIResponse.error(code=10007, message='查询绑定手机失败')


# 查询绑定邮箱
@user_blueprint.route('/get-bind-email', methods=['GET'])
def get_email():
    email = query_email()
    if email:
        return APIResponse.ok(data=email)
    return APIResponse.error(code=10008, message='查询绑定邮箱失败')


# 绑定手机
@user_blueprint.route('/bind-phone', methods=['POST'])
def bind_phone():
    data = request.get_json()
    phone = data.get('phone')
    bind = set_phone(phone)
    print(f"绑定手机：{bind}")
    return APIResponse.ok(data=bind)


# 绑定邮箱
@user_blueprint.route('/bind-email', methods=['POST'])
def bind_email():
    data = request.get_json()
    email = data.get('email')
    bind = set_email(email)
    print(f"绑定邮箱：{bind}")
    return APIResponse.ok(data=bind)
